package cn.iocoder.yudao.module.train.controller.admin.coursecancel;

import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.CourseCancelDTO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.train.controller.admin.coursecancel.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.coursecancel.CourseCancelDO;
import cn.iocoder.yudao.module.train.service.coursecancel.CourseCancelService;

@Tag(name = "管理后台 - 课程消课记录")
@RestController
@RequestMapping("/train/course-cancel")
@Validated
public class CourseCancelController {

    @Resource
    private CourseCancelService courseCancelService;

    @PostMapping("/create")
    @Operation(summary = "创建课程消课记录")
    @PreAuthorize("@ss.hasPermission('train:course-cancel:create')")
    public CommonResult<Integer> createCourseCancel(@Valid @RequestBody CourseCancelSaveReqVO createReqVO) {
        return success(courseCancelService.createCourseCancel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程消课记录")
    @PreAuthorize("@ss.hasPermission('train:course-cancel:update')")
    public CommonResult<Boolean> updateCourseCancel(@Valid @RequestBody CourseCancelSaveReqVO updateReqVO) {
        courseCancelService.updateCourseCancel(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课程消课记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('train:course-cancel:delete')")
    public CommonResult<Boolean> deleteCourseCancel(@RequestParam("id") Integer id) {
        courseCancelService.deleteCourseCancel(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除课程消课记录")
                @PreAuthorize("@ss.hasPermission('train:course-cancel:delete')")
    public CommonResult<Boolean> deleteCourseCancelList(@RequestParam("ids") List<Integer> ids) {
        courseCancelService.deleteCourseCancelListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课程消课记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('train:course-cancel:query')")
    public CommonResult<CourseCancelRespVO> getCourseCancel(@RequestParam("id") Integer id) {
        CourseCancelDO courseCancel = courseCancelService.getCourseCancel(id);
        return success(BeanUtils.toBean(courseCancel, CourseCancelRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程消课记录分页")
    @PreAuthorize("@ss.hasPermission('train:course-cancel:query')")
    public CommonResult<PageResult<CourseCancelRespVO>> getCourseCancelPage(@Valid CourseCancelPageReqVO pageReqVO) {
        PageResult<CourseCancelDTO> pageResult = courseCancelService.getCourseCancelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CourseCancelRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程消课记录 Excel")
    @PreAuthorize("@ss.hasPermission('train:course-cancel:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCourseCancelExcel(@Valid CourseCancelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CourseCancelDTO> list = courseCancelService.getCourseCancelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "课程消课记录.xls", "数据", CourseCancelRespVO.class,
                        BeanUtils.toBean(list, CourseCancelRespVO.class));
    }

}
